
const HtmlWebpackPlugin = require('html-webpack-plugin')

const { resolve } = require('path')
const htmlPublicPath = resolve(__dirname, 'build')
function RooPath(path) {
  return resolve(__dirname, path)
}

module.exports = {
  // 单入口 - 就一个 bundle
  entry: RooPath('./src/js/index.js'),
  // entry: {
  //   // 多入口 ****
  //   main: RooPath('./src/js/index.js'),
  //   test: RooPath('./src/js/test.js')
  // },
  output: {
    // [name]: 取文件名
    filename: 'js/[name].[contenthash:10].js',
    path: htmlPublicPath,
    publicPath: './',
  },
  optimization: {
    /*
      可以将 node_modules 中代码单独打包一个chunk，最终输出
    */
    // https://webpack.js.org/configuration/optimization/#optimizationsplitchunks
    splitChunks: { // webpack v4+
      // https://webpack.js.org/plugins/split-chunks-plugin/
      chunks: 'all',
    }
  },
  // optimization: {
  //   splitChunks: {
  //     chunks: 'async',
  //     minSize: 40,
  //     minRemainingSize: 0,
  //     maxSize: 0,
  //     minChunks: 1,
  //     maxAsyncRequests: 30,
  //     maxInitialRequests: 30,
  //     automaticNameDelimiter: '~',
  //     enforceSizeThreshold: 50000,
  //     cacheGroups: {
  //       defaultVendors: {
  //         test: /[\\/]node_modules[\\/]/,
  //         priority: -10,
  //         reuseExistingChunk: true
  //       },
  //       default: {
  //         minChunks: 2,
  //         priority: -20,
  //         reuseExistingChunk: true
  //       }
  //     }
  //   }
  // },
  plugins: [
    new HtmlWebpackPlugin({
      template: RooPath('./src/index.html')
    }),
  ],
  mode: 'production', // 生成模式
}